package mo.leetcode_top;

import java.util.HashSet;
import java.util.Set;

public class L202 {

    private Set<Integer> set = new HashSet<>();

    public boolean isHappy(int n) {

        int res = 0;
        while (n > 0) {
            int x = n % 10;
            n = n / 10;
            res += x * x;
        }
        if (res == 1) {
            return true;
        } else {
            if (set.contains(res)) {
                return false;
            }
            set.add(res);
            return isHappy(res);
        }
    }

    public static void main(String[] args) {
        L202 test = new L202();
        System.out.println(test.isHappy(19));
    }

}
